

import it.lupo.core.LupoSession;
import it.lupo.core.gui.controller.LoginController;
import it.lupo.core.gui.controller.SearchController;
import it.lupo.model.User;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
 

public class testMainApp extends Application{

    private static final String ICON_FILE = "icon.png";

	private Stage currentStage;     
    
	   private static testMainApp instance;
	   
	   
	   public testMainApp() {
	       instance = this;
	       LupoSession.getCurrent().login(new User("root", "password", null));
	       System.out.println("javafx version "+com.sun.javafx.runtime.VersionInfo.getRuntimeVersion());
	       
	   }	

	   
	   public static testMainApp getInstance() {
		if(instance==null)
			instance=new testMainApp();
		return instance;
	   }
	

    
    
    @Override
    public void start(Stage primaryStage) {
        this.currentStage = primaryStage;
        this.currentStage.setTitle("Lupo");
        
        try {
			currentStage.getIcons().add(new Image(new FileInputStream(new File(ICON_FILE))));
		} catch (Exception e) {
			e.printStackTrace();
		}

        try {
			change(SearchController.FXML_FILE);
		} catch (Exception e) { 
			e.printStackTrace();
		}

    }

//    public Stage toHome()
//    {
//    	
//    }
    
    public void change(String stage_fxml) throws Exception
    {
    	URL url = new File(stage_fxml).toURL();
        FXMLLoader loader = new FXMLLoader(url);
//        rootLayout = (AnchorPane) loader.load();
         
        Scene scene = new Scene((Parent) loader.load());
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
         
       System.out.println("DIM = "+dim.getWidth()); 
      
        
        currentStage.setScene(scene);
        currentStage.show();        
        System.out.println("DIM = "+scene.getWidth());
        currentStage.setX(((double)dim.getWidth()-currentStage.getWidth())/2);
        currentStage.setY(((double)dim.getHeight()-currentStage.getHeight())/2);
        System.out.println("Current "+currentStage.getTitle()+" "+currentStage.getClass());
         
    }
    
    public Stage getCurrentStage() {
        return currentStage;
    }

    /**
     * Shows the person overview scene.
     */
    public void showPersonOverview() {
//        try {
            // Load the fxml file and set into the center of the main layout
//            FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/PersonOverview.fxml"));
//            AnchorPane overviewPage = (AnchorPane) loader.load();
//            
//            rootLayout.setCenter(overviewPage);

//        } catch (IOException e) {
//            // Exception gets thrown if the fxml file could not be loaded
//            e.printStackTrace();
//        }
    }
    
//    private Parent replaceSceneContent(String fxml) throws Exception {
//        Parent page = (Parent) FXMLLoader.load(MainApp.class.getResource(fxml), null, new JavaFXBuilderFactory());
//        Scene scene = currentStage.getScene();
//        if (scene == null) {
//            scene = new Scene(page, 700, 450);
//            scene.getStylesheets().add(MainApp.class.getResource("demo.css").toExternalForm());
//            currentStage.setScene(scene);
//        } else {
//        	currentStage.getScene().setRoot(page);
//        }
//        currentStage.sizeToScene();
//        return page;
//    }

    public static void main(String[] args) {
        launch(args);
    }
}